package com.wwz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.constant.ProjectConst;
import com.wwz.entity.Company;
import com.wwz.entity.Result;
import com.wwz.exception.EcoBootException;
import com.wwz.mapper.CompanyMapper;
import com.wwz.query.CompanyQuery;
import com.wwz.service.ICompanyService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Override
    public IPage<Company> toPage(CompanyQuery query) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        // 模糊查询
        wrapper.eq(Company::getState,ProjectConst.STATUS_OK);
        wrapper.orderByDesc(Company::getCreateTime);
        IPage<Company> iPage = this.page(query.toPage(), wrapper);
        return iPage;
    }

    @Override
    public void addOrUpData(Company company) {
        if (StrUtil.isAllEmpty(company.getName(),company.getLegalRepresentative())){
            throw new EcoBootException("字段不能为空");
        }
        Company data = getById(company.getId());
        // 为空 新增
        if (data == null){
            company.setId(StrUtil.uuid());
            company.setAuditState(ProjectConst.AUDIT_STATE_OK);
            company.setCreateTime(new Date());
            save(company);
            return;
        }

        // 存在数据 修改
        updateById(company);
    }

    @Override
    public void deleteById(String id) {
        Company company = getOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getId, id), false);

        if (company == null){
            throw new EcoBootException("该条数据不存在");
        }
        // 逻辑删除
        company.setState(ProjectConst.STATUS_NO);
        updateById(company);
    }
}
